//app.js
App({
    onLaunch: function () {
        //调用API从本地缓存中获取数据
        var logs = wx.getStorageSync('logs') || [];
        logs.unshift(Date.now());
        wx.setStorageSync('logs', logs);
    },
    getAccessToken: function (cb) {
        var that = this;
        if (that.accessToken) {
            typeof cb == "function" && cb(this.accessToken);
        } else {
            wx.request({
                url: 'https://api.cnblogs.com/token', //仅为示例，并非真实的接口地址
                method: 'POST',
                data: {
                    username: 'g9dDrMoMV4DzZrOAMcAnCegdisk20T6ddm6ySsUrbpdeID7bL9iV5QcjRISv/2/QfwK5SGClWe7YX8FAghw0jZYTtjUnB1kyPN5H712oKmxiVKMLzm35Yd6kYu5dql/FsCGujeEuzIxN9Mu92g1Xve92jZDz8nLIPe4vhk3nt1I=',
                    password: 'kTYhPUx6wxSTwWicd+7w5vhIgx6m0QzMYR5LCWmgICSmy+qWJbjpqi4U2/U0i0dSxFVnF9f1iyW9poPbW1xgHcQ46SGwQSRLv2SNnqwMEWph5DzGnMQzE1w7C5hVDMS2YwS8RgAO+U5euG778uCWAu/SejzeCf+37HqJrjN1IzQ=',
                    grant_type: 'password'
                },
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'Authorization': 'Basic NTMxMmI0ZDMtODk3YS00ZWFjLTlkZmMtZWJiYzNkMTYwYmY1Ok5MdWtlRHRlMDktMkV5ZjBhbnFsZXpOeVZGTTVSb0N0SjF0d3dLTXZRalcwdFNWYmlzR3Y2MXRjblVnbjFfaHMyWTZrcU8yQ0xheDVlOTIw'
                },
                success: function (res) {
                    that.accessToken = res.data.accessToken;
                    typeof cb == "function" && cb(that.globalData.accessToken);
                },
                fail: function () {
                    console.log('获取token失败!');
                }
            });
        }
    },
    getUserInfo: function (cb) {
        var that = this;
        var accessToken = that.accessToken;
        if (this.user) {
            typeof cb == "function" && cb(this.user);
        } else {
            wx.request({
                url: 'https://api.cnblogs.com/api/Users', //仅为示例，并非真实的接口地址
                method: 'GET',
                data: {
                },
                header: {
                    'Authorization': 'Bearer ' + accessToken
                },
                success: function (res) {
                    that.user = res.data;
                    typeof cb == "function" && cb(that.user);
                },
                fail: function () {
                    console.log('获取用户信息失败!');
                }
            });
        }
    },
    user: null,
    accessToken: 'oIyW4j2T6YdaCs6ou-4iosGlp_2E4rs-IV7WRYj8kYJiU1np6jaL_HMZ9daHoVGaTFaF63mWgf80-7VqA3KGHPh7Nkp4gisDz26iTgc3i1IOQeZZMc15W2ytqi1SslFf9qlaIqY3QW7bHUEhY3ViaA052_15JX5yLNhJUuo1TW6TioDrZTp1w4378DBAh9zlyo9RColn6w3e3_3xXaoU3g'
});
